
const mongoose = require('mongoose');
const _ = require('lodash');
var customerService = require('../service/customer.service');

module.exports={
    addCustomer:addCustomer,
    findByCustomerName:findByCustomerName,
    findByCustomerNameAndPwd:findByCustomerNameAndPwd,
    updateCustomer:updateCustomer,
    addAddress:addAddress,
    deleteAddress:deleteAddress,
    isLogin:isLogin,
    findAddressList:findAddressList,
    Logout:Logout
};

function addCustomer(req,res,next){
    var customerInfo = req.body;
    var cName = customerInfo.cName;
    if(_.isEmpty(cName)){
        res.json({result:false,errMsg:'Name can not be empty!'});
    }else {
        customerService.findByCustomerName(cName,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                if(_.isEmpty(data)){
                    customerService.addCustomer(customerInfo,function(err,data){
                        if(err){
                            res.json({result:false,errMsg:Error(err)});
                        }else {
                            res.json({result:true,data:data});
                        }
                    });
                }
                else {
                    res.json({result: false, errMsg: 'Customer name already exist!'});
                }
            }
        });
    }
}

function  updateCustomer(req,res,next) {
    var customerInfo = req.body;
    var _id = customerInfo._id;
    if(_.isEmpty(_id)){
        res.json({result:false,errMsg:'_id can not be empty!'});
    }else {
        customerService.findByCustomerId(_id,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                if(_.isEmpty(data)){
                    res.json({result: false, errMsg: 'Customer not exist!'});
                }
                else {
                    customerService.updateCustomer(customerInfo,function(err,data){
                        if(err){
                            res.json({result:false,errMsg:Error(err)});
                        }else {
                            req.session.customerInfo=customerInfo;
                            res.json({result:true,data:data});

                        }
                    });
                }
            }
        });
    }
}


function findByCustomerName(req,res,next){
    var cName = req.params.cName;
    if(_.isEmpty(cName)){
        res.json({result:false,errMsg:'Name can not be empty!'});
    }else {
        customerService.findByCustomerName(cName,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                if(_.isEmpty(data)){
                    res.json({result: false, errMsg:'Customer not exist or password wrong!'});
                }
                else {
                    res.json({result: true, data: data});
                }
            }
        });
    }
}


function findByCustomerNameAndPwd(req,res,next){
    var cName = req.params.cName;
    var cpwd = req.params.cpwd;
    if(_.isEmpty(cName)||_.isEmpty(cpwd)){
        res.json({result:false,errMsg:'Name and password can not be empty!'});
    }else {
        customerService.findByCustomerNameAndPwd(cName,cpwd,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                if(_.isEmpty(data)){
                    res.json({result: false, errMsg:'Customer not exist or password wrong!'});
                }
                else {
                    console.log("function Login cookie"+JSON.stringify(req.cookie))
                    console.log("function Login before session"+JSON.stringify(req.session));
                    req.session.customerInfo = data;
                    console.log("function Login after session"+JSON.stringify(req.session));
                    res.json({result: true, data: data});

                    // req.session.customerInfo = data._doc;

                }
            }
        });
    }
}


function isLogin(req,res,next){
    console.log("function isLogin cookie"+JSON.stringify(req.cookie));
    console.log("function isLogin sesstion"+JSON.stringify(req.session));
    res.json({customerInfo:req.session.customerInfo});
}


function Logout(req,res,next){
    console.log("function isLogin cookie"+JSON.stringify(req.cookie));
    console.log("function isLogin sesstion before"+JSON.stringify(req.session));
    delete req.session.customerInfo;
    console.log("function isLogin sesstion after"+JSON.stringify(req.session));
    res.json({result:true});
}

function addAddress(req,res,next){
    var customerId = req.body.customerId;
    var address = req.body.address;
    if(_.isEmpty(address)){
        res.json({result:false,errMsg:'Address can not be empty!'});
    }else {
        customerService.addAddress(customerId,address,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                customerService.findAddressList(customerId,function(err,data) {
                    if(err){
                        res.json({result:false,errMsg:Error(err)});
                    }else{
                        res.json({result: true, data: data._doc.address[data._doc.address.length-1]._id});
                    }
                });
            }
        });
    }
}

function deleteAddress(req,res,next){
    var customerId = req.params.customerId;
    var addressId = req.params.addressId;
    if(_.isEmpty(addressId)){
        res.json({result:false,errMsg:'AddressId can not be empty!'});
    }else {
        customerService.deleteAddress(customerId,addressId,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                if(_.isEmpty(data)){
                    res.json({result: false, errMsg:'Customer not exist or password wrong!'});
                }
                else {
                    res.json({result: true, data: data});
                }
            }
        });
    }
}

function findAddressList(req,res,next){
    var customerId = req.params.customerId;
    if(_.isEmpty(customerId)){
        res.json({result:false,errMsg:'customerId can not be empty!'});
    }else {
        customerService.findAddressList(customerId,function(err,data){
            if(err){
                res.json({result:false,errMsg:Error(err)});
            }else {
                res.json({result: true, data: data});
            }
        });
    }
}


